#include <stdio.h>
int outt[36][14] = {0};
char week[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int month[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int get_whatday(int year)
{                          //用于获取当前输入年份的第一天是星期几
    int firstday_1970 = 4; //1970年第一天为星期四
    int total = 0;         //记录从1970年1月1日到当前年的1月1日的天数
    int i;
    for (i = 1970; i < year; ++i)
    { //累计天数
        total += ((0 == i % 4 && i % 100 != 0 || 0 == i % 400) ? 366 : 365);
    }
    return (total + firstday_1970) % 7;
}
int get_out(int firstday, int year)
{ //给用于输出的二维数组赋值
    int i, j, k;
    int *mont = (0 == year % 4 && year % 100 != 0 || 0 == year % 400) ? month[1] : month[0];
    printf("mon[1]=%d\n", mont[1]);
    int total = firstday;  //累计天数用于计算星期数
    int current_month = 0; //记录当前月份-1
    int accumulative_day;  //累计当前月份的天数
    for (i = 0; i < 36; i += 6)
    {
        int tmp = i + 6;
        int firstday_month = total % 7; //记录每个月的第一天为星期几
        for (j = i, accumulative_day = 1; j < tmp; ++j)
        {
            for (k = 0; k < 7 && accumulative_day <= mont[current_month]; ++k, ++accumulative_day)
            {
                if (1 == accumulative_day)
                {
                    k += total % 7;
                }
                outt[j][k] = accumulative_day;
                ++total;
            }
        }
        ++current_month;
        for (j = i, accumulative_day = 1; j < tmp; ++j)
        {
            for (k = 7; k < 14 && accumulative_day <= mont[current_month]; ++k, ++accumulative_day)
            {
                if (1 == accumulative_day)
                {
                    k += total % 7;
                }
                outt[j][k] = accumulative_day;
                ++total;
            }
        }
        ++current_month;
    }
}
void print(int year)
{
    int i, j, k;
    int month = 1;
    for (i = 0; i < 27; ++i)
        putchar(' ');
    printf("%d年\n", year);
    for (i = 0; i < 36; ++i)
    {
        if (0 == i % 6)
        {
            for (j = 0; j < 2; ++j)
            {
                for (k = 0; k < 11; ++k)
                    putchar(' ');
                printf("%2d月", month++);
                if (0 == j)
                    for (k = 0; k < 16; ++k)
                        putchar(' ');
            }
            putchar('\n');
            for (k = 0; k < 14; ++k)
            {
                if (7 == k)
                    printf("   ");
                printf("%s ", week[k % 7]);
            }
            printf("\n");
        }
        for (j = 0; j < 14; ++j)
        {
            if (7 == j)
                printf("   ");
            if (outt[i][j] != 0)
                printf("%2d  ", outt[i][j]);
            else
                printf("    ");
        }
        printf("\n");
    }
}
int main()
{
    int year;
    printf("请输入想要获取日历的年份：");
    fflush(stdout);
    scanf("%d", &year);
    //initialize
    int firstday = get_whatday(year);
    get_out(firstday, year);
    system("clear");
    print(year);
    //输出流程
    return 0;
}